package logic

import (
	"context"
	"errors"
	"good/model/mysql"
	"good/model/redis"
	"gorm.io/gorm"

	"good/good"
	"good/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodShowLogic {
	return &GoodShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodShowLogic) GoodShow(in *good.GoodShowRequest) (*good.GoodShowResponse, error) {
	// todo: add your logic here and delete this line
	m := mysql.Good{
		Model: gorm.Model{ID: uint(in.Id)},
	}
	show, err := m.GoodShow()
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, err
	}
	stock, err := redis.LengthShopStock(int(in.Id)) //查询虚拟库存长度
	if err != nil {
		return nil, err
	}
	logx.Error(err)
	return &good.GoodShowResponse{
		LenShopStock: stock,
		GoodShow: &good.GoodShow{
			GoodTitle:  show.GoodTitle,
			GoodPrice:  show.GoodPrice,
			GoodStock:  show.GoodStock,
			GoodCover:  show.GoodCover,
			GoodStatus: show.GoodStatus,
			Id:         int64(show.ID),
		}}, nil
}
